<?php

/**
 * 新闻
 */

namespace app\api\controller\news;

use app\api\controller\Common;

class News extends Common
{
    protected $data;
    public function initialize()
    {
        parent::initialize();
        $this->data = $this->request->except(['sign', 'accessKey', 'token']);
    }

    //案例一级分类
    public function category1List()
    {
        $brand_id = input('brand_id', 0);
        $list = db('news_category')->alias('a')
            ->field('a.id,a.title,a.title_en')
            ->where(['del'=>0, 'pid'=>$brand_id])->order('sort', 'desc')->select();
        return $this->showSuccess('案例一级分类列表', $list);
    }

    //案例二级分类
    public function category2List()
    {
        $category = input('category_id', 0);
        $brand_id = db('news_category')->where(['id' => $category])->value('pid');
        $brand_name = db('news_category')->where(['id' => $brand_id])->value('title');

        $list = db('news_category')->alias('a')
            ->field('a.id,a.title,a.title_en')
            ->where(['del' => 0, 'pid' => $category])->order('sort', 'desc')->select();
        
        $expend = ['brand_name'=> $brand_name];
        return $this->showSuccess('案例二级分类列表', $list, $expend);
    }

    //栏目下文章列表
    public function list()
    {
        $type = input('type_id', 0);
        $brand_id = input('brand_id', 0);
        $category1_id = input('category1_id', 0);
        $category2_id = input('category2_id', 0);
        $keyword = input('keyword', '');
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 10;
        $paginate = ['list_rows' => $pageSize, 'page' => $page];

        $where = [];
        $where[] = ['del', '=', 0];
        if($type) {
            $where[] = ['type', '=', $type];
        }
        if ($brand_id) {
            $where[] = ['brand_id', '=', $brand_id];
        }
        if ($keyword) {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }
        if ($category1_id) {
            $where[] = ['category1', '=', $category1_id];
        }
        if ($category2_id) {
            $where[] = ['category2', '=', $category2_id];
        }
       

        $list = db('news')
            ->field('id, title, desc, pic')
            ->where($where)->order('update_time', 'desc')->paginate($paginate)->toArray();
        return $this->showSuccess('文章列表', $list);
    }

    public function detail()
    {
        $id = input('id');
        $detail = db('news')->field('id,title,pic,video,content,create_time')->where('id', $id)->find();
        if ($detail) {
            db('news')->where('id', $id)->setInc('hit'); // 浏览量+1
            $detail['format_data'] = $this->format_date($detail['create_time']);
            $detail['content'] = str_replace('src="', 'src="' . config('img_host'), $detail['content']);

            // 关联的商品
            $news_goods = db('news_goods')->field('id,goods_id,name,pic,desc,sale_price')->where('news_id', $id)->select();
            $detail['goods'] = $news_goods;

            $this->return = ['code' => 'SUCCESS', 'status' => 200, 'message' => '成功', 'data' => $detail];
        } else {
            $this->return = ['code' => 'GET_ERROR', 'status' => 200, 'message' => '无数据'];
        }
        return $this->show($this->return);
    }

    /**
     * 相关推荐
     */
    public function more()
    {
        $id = input('id');
        $category = db('news')->where('id', $id)->value('category');
        if ($category) {
            $res = db('news')->field('id,title,pic')->where('category', $category)->order('hit', 'desc')->limit(4)->select();
            $this->return = ['code' => 'SUCCESS', 'status' => 200, 'message' => '成功', 'data' => $res];
        } else {
            $this->return = ['code' => 'GET_ERROR', 'status' => 200, 'message' => '无数据'];
        }
        return $this->show($this->return);
    }

    /**
     * 收藏/取消
     */
    public function newsCollect()
    {
        $this->checkUserLogin(); // 验证登录
        $data = $this->data;
        $uid = $this->user['id'];
        $data['uid'] = $uid;
        $data['create_time'] = date("Y-m-d H:i:s", time());
        $is_collect = db('news_collect')->where(['did' => $data['did'], 'uid' => $uid])->find();
        if ($is_collect) {
            $res = db('news_collect')->where(['did' => $data['did'], 'uid' => $uid])->delete();
            if ($res) {
                $this->return = ['status' => 200, 'code' => 'SUCCESS', 'message' => '成功', 'data' => 2];
            } else {
                $this->return = ['status' => 200, 'code' => 'GET_ERROR', 'message' => '失败', 'data' => 4];
            }
        } else {
            $res = db('news_collect')->insert($data);
            if ($res) {
                $this->return = ['status' => 200, 'code' => 'SUCCESS', 'message' => '成功', 'data' => 1];
            } else {
                $this->return = ['status' => 200, 'code' => 'GET_ERROR', 'message' => '失败', 'data' => 3];
            }
        }
        return $this->show($this->return);
    }

    /**
     * 7.6 我收藏的新闻列表
     */
    public function myCollect()
    {
        $this->checkUserLogin();
        $uid = $this->user['id'];
        $page = $this->data['page'] ? $this->data['page'] : 1;
        $pageSize = $this->data['limit'] ? $this->data['limit'] : 20;
        $paginate = ['list_rows' => $pageSize, 'page' => $page];
        $res = db('news_collect')->alias('a')
            ->field('b.id,b.title,b.pic,b.hit')
            ->join('news b', 'a.did = b.id')
            ->where(['a.uid' => $uid])->order('a.id DESC')->paginate($paginate)->toArray();

        return $this->showSuccess('我收藏的新闻列表', $res);
    }
}
